﻿<h1>Keywords and Identifiers in Go</h1>

<p>
This article will introduce keywords and identifiers in Go.
</p>

<a class="anchor" id="keyword"></a>
<h3>Keywords</h3>

<div>
<p>
Keywords are the special words which help compilers understand and parse user code.
</p>

Up to now (Go 1.13), Go has only 25 keywords:

<pre class="line-numbers fixed-width"><code class="language-go">break     default      func    interface  select
case      defer        go      map        struct
chan      else         goto    package    switch
const     fallthrough  if      range      type
continue  for          import  return     var
</code></pre>

They can be categorized as four groups:
<ul>
<li>
	<code>const</code>, <code>func</code>, <code>import</code>, <code>package</code>, <code>type</code> and <code>var</code>
	are used to declare all kinds of code elements in Go programs.
</li>
<li>
	<code>chan</code>, <code>interface</code>, <code>map</code> and <code>struct</code>
	are used as parts in some composite type denotations.
</li>
<li>
	<code>break</code>, <code>case</code>, <code>continue</code>, <code>default</code>, <code>else</code>, <code>fallthrough</code>,
	<code>for</code>, <code>goto</code>, <code>if</code>, <code>range</code>, <code>return</code>, <code>select</code> and <code>switch</code>
    are used to control flow of code.
</li>
<li>
	<code>defer</code> and <code>go</code> are also control flow keywords, but in other specific manners.
	They modify function calls, which we'll talk about in
	<a href="control-flows-more.html">this article</a>.
</li>
</ul>

<p>
These keywords will be explained in details in other articles.
</p>
</div>

<a class="anchor" id="identifier"></a>
<h3>Identifiers</h3>

<div>
An identifier is a token which must be composed of Unicode letters,
Unicode digits (Number category <i>Nd</i> in Unicode Standard 8.0)
and <code>_</code> (underscore), and start with
either an Unicode letter or <code>_</code>.
Here,
<ul>
<li>
	Unicode letters mean the characters defined in the Letter categories
	<i>Lu</i>, <i>Ll</i>, <i>Lt</i>, <i>Lm</i>, or <i>Lo</i>
	of <a href="http://www.unicode.org/versions/Unicode8.0.0/">The Unicode Standard 8.0</a>.
</li>
<li>
	Unicode digits mean the characters defined
	in the Number category <i>Nd</i> of The Unicode Standard 8.0.
</li>
</ul>
</div>

<div class="note">
<p>
<b>keywords</b> can not be used as identifiers.
</p>
</div>

<p>
Identifier <code>_</code> is a special identifier, it is called <b>blank identifier</b>.
</p>

<p>
Later we will learn that all names of types, variables, constants, labels,
package names and package import names must be identifiers.
</p>

<p>
An identifier starting with an
<a href="http://www.fileformat.info/info/unicode/category/Lu/list.htm">Unicode upper case letter</a>
is called an <b>exported identifier</b>.
The word <b><i>exported</i></b> can be interpreted as
<b><i>public</i></b> in many other languages.
The identifiers which don't start with an Unicode upper case letter are called non-exported identifiers.
The word <b><i>non-exported</i></b> can be interpreted as
<b><i>private</i></b> in many other languages.
Currently (Go 1.13), eastern characters are viewed as non-exported letters.
Sometimes, non-exported identifiers are also called unexported identifiers.
</p>

<div>
Here are some legal exported identifiers:
<pre class="line-numbers fixed-width"><code class="language-go">Player_9
DoSomething
VERSION
Ĝo
Π
</code></pre>

Here are some legal non-exported identifiers:
<pre class="line-numbers fixed-width"><code class="language-go">_
_status
memStat
book
π
一个类型
변수
エラー
</code></pre>

And here are some tokens which are illegal to be used as identifiers:
<pre class="line-numbers fixed-width"><code class="language-go">// Starting with a Unicode digit.
123
3apples

// Containing Unicode characters not
// satisfying the requirements.
a.b
*ptr
$name
a@b.c

// These are keywords.
type
range
</code></pre>

</div>
